/**
 * 数字格式化输出，避免因为数字精度问题出现长小数的问题
 * @param num 数字
 * @returns
 */
export const formatDouble = (num?: number): string => {
  if (num == null) return '0'
  return num.toFixed(2).replace('.00', '')
}

/**
 * 性别描述
 * @param sex 性别
 * @returns
 */
export const sexDesc = (sex: number): string => {
  return sex == 1 ? '男' : '女'
}

/**
 * 证件类别描述
 * @param type 证件类别
 * @returns
 */
export const visitorTypeDesc = (type: number): string => {
  return type == 1 ? '身份证' : '护照'
}

/**
 * 订单状态描述
 * @param status 订单状态
 * @returns
 */
export const enrollStatusDesc = (status: number): string => {
  if (status == 1) return '待确认'
  if (status == 2) return '已确认'
  if (status == 3) return '已付款'
  return '已取消'
}

/**
 * 身份证验证
 */
export const isValidID = (id: string): boolean => {
  if (!id || id.length !== 18) return false

  const weight = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
  const checkCode = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2']

  let sum = 0
  for (let i = 0; i < 17; i++) {
    sum += parseInt(id[i]) * weight[i]
  }

  const index = sum % 11
  if (checkCode[index] !== id[17].toUpperCase()) return false

  return true
}
